package com.example.gomoku;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class GomokuActivity extends AppCompatActivity {

    private GomokuView gomokuView;
    private Button resetButton;
    private Button undoButton;
    private Button pauseButton;
    private TextView blackCount;
    private TextView whiteCount;
    private Button backButton;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gomoku);

        gomokuView = findViewById(R.id.gomokuView);
        resetButton = findViewById(R.id.resetButton);
        undoButton = findViewById(R.id.undoButton);
        pauseButton = findViewById(R.id.pauseButton);
        backButton = findViewById( R.id.back );
        blackCount = findViewById( R.id.blackCount );
        whiteCount = findViewById(R.id.whiteCount);

        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gomokuView.resetGame();
                updatePieceCount();
            }
        });

        undoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gomokuView.undoMove();
                updatePieceCount();
            }
        });

        pauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gomokuView.pauseGame();
                if (gomokuView.isPaused()) {
                    pauseButton.setText("继续");
                } else {
                    pauseButton.setText("暂停");
                }
            }
        });
/*        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GomokuActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });*/

        gomokuView.setOnWinListener(new GomokuView.OnWinListener() {
            @Override
            public void onWin(boolean isBlack) {
                String winner = isBlack ? "黑棋" : "白棋";
                Toast.makeText(GomokuActivity.this, winner + "胜利！", Toast.LENGTH_LONG).show();
            }
        });

        gomokuView.setOnPiecePlacedListener(new GomokuView.OnPiecePlacedListener() {
            @Override
            public void onPiecePlaced() {
                updatePieceCount();
            }
        });
    }

    private void updatePieceCount() {
        blackCount.setText("黑棋: " + gomokuView.getBlackCount());
        whiteCount.setText("白棋: " + gomokuView.getWhiteCount());
    }
}